package com.itzxx.huazhun.ui.record.local

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import com.chad.library.adapter4.QuickAdapterHelper
import com.chad.library.adapter4.loadState.LoadState
import com.chad.library.adapter4.loadState.trailing.TrailingLoadStateAdapter
import com.itzxx.huazhun.R
import com.itzxx.huazhun.data.dao.CollectionEntity
import com.itzxx.huazhun.databinding.FragmentRecordLocalBinding
import com.itzxx.huazhun.ui.record.RecordFragment
import com.itzxx.huazhun.view.adapter.CustomLoadMoreAdapter
import com.itzxx.library_base_ktx.base.fragment.BaseVmDbFragment
import com.itzxx.library_base_ktx.ext.view.addLinearAdapter


class RecordLocalFragment : BaseVmDbFragment<RecordLocalViewModel,FragmentRecordLocalBinding>() {

    private lateinit var recordAdapter: RecordLocalAdapter
    private lateinit var helper: QuickAdapterHelper
    private lateinit var loadMoreAdapter: CustomLoadMoreAdapter

    override fun initView(savedInstanceState: Bundle?) {
        initRecyclerView()
        mDatabind.btSearchCurve.setOnClickListener {

        }
        val adapter = ArrayAdapter(
            mActivity, android.R.layout.simple_spinner_item, mViewModel.sceneTypeLiveData
        )
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        mDatabind.tvSearchType.adapter = adapter
        mDatabind.tvSearchType.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {
                // 可选的回调，一般不需要
            }

            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                mViewModel.currentCheckSceneTypePosition = position

            }
        }
        mDatabind.tvSearchType.clearAnimation()
        mDatabind.btSearchCurve.setOnClickListener {
            mViewModel.onValuateEvent(recordAdapter.items)
        }
    }

    private fun initRecyclerView() {
        recordAdapter = RecordLocalAdapter()
        loadMoreAdapter = CustomLoadMoreAdapter()
        loadMoreAdapter.setOnLoadMoreListener(object : TrailingLoadStateAdapter.OnTrailingListener {
            override fun onLoad() {
                // 执行加载更多的操作，通常都是网络请求
                mViewModel.getList(false)
            }

            override fun onFailRetry() {
                // 加载失败后，点击重试的操作，通常都是网络请求
                mViewModel.getList(false)
            }

            override fun isAllowLoading(): Boolean {
                return true
            }
        })
        helper = QuickAdapterHelper.Builder(recordAdapter)
            .setTrailingLoadStateAdapter(loadMoreAdapter)
            .build()
        helper.trailingLoadState = LoadState.None
        mDatabind.rvList.addLinearAdapter(helper.adapter)
        recordAdapter.addOnItemChildClickListener(R.id.upload){ adapter, view, position ->
            val item = adapter.getItem(position) as CollectionEntity
            mViewModel.uploadProbeCollection(item)
        }
    }

    override fun lazyLoadData() {
        mViewModel.getList(true)
    }

    override fun createObserver() {
        mViewModel.run {
            listDataUiStateLiveData.observe(this@RecordLocalFragment) {
                if (it.isRefresh) {
                    (parentFragment as RecordFragment).finishRefresh()
                    recordAdapter.submitList(it.listData)
                } else {
                    recordAdapter.addAll(it.listData)
                }

                if (it.hasMore) {
                    helper.trailingLoadState = LoadState.NotLoading(false)
                } else {
                    loadMoreAdapter.setFirstNotFullScreen(it.isRefresh)
                    helper.trailingLoadState = LoadState.NotLoading(true)
                }
            }

            uploadRecodLiveData.observe(this@RecordLocalFragment){
                    recordAdapter.removeItem(it)
            }
        }
    }

    override fun databind2ViewModel() {
        super.databind2ViewModel()
        mDatabind.viewmodel = mViewModel
    }

    override fun isApplyDefaultWaitDialog(): Boolean {
        return true
    }
}